import plotly.offline as pyo
from plotly.graph_objs import *
import chart_studio.plotly as py
import pandas as pd
from pandas import DataFrame
pyo.offline.init_notebook_mode()
LifePopGDP = pd.read_csv(r"../Data/GapminderData.csv", index_col = 0)
LifePopGDP.head()
| Continent | Country | Life Expectancy | GDP per Capita | Population | |
|---|---|---|---|---|---|
| 0 | Africa | Algeria | 70.874 | 12779.0 | 36488669.0 |
| 1 | Africa | Angola | 51.498 | 7230.0 | 20039259.0 |
| 2 | Africa | Benin | 59.165 | 1685.0 | 9775152.0 |
| 3 | Africa | Botswana | 47.152 | 14905.0 | 2030287.0 |
| 5 | Africa | Burundi | 53.637 | 737.0 | 8899077.0 |
LifePopGDP['text'] = LifePopGDP.apply(lambda x:
"<b>{}</b><br>Life Expectancy: {:.0f} years<br>GDP/Capita: ${:,.0f}<br>Population: {:,.0f}".format(x['Country'], x['Life Expectancy'], x['GDP per Capita'], x['Population']), axis = 1)
LifePopGDP.head()
| Continent | Country | Life Expectancy | GDP per Capita | Population | text | |
|---|---|---|---|---|---|---|
| 0 | Africa | Algeria | 70.874 | 12779.0 | 36488669.0 | <b>Algeria</b><br>Life Expectancy: 71 years<br... |
| 1 | Africa | Angola | 51.498 | 7230.0 | 20039259.0 | <b>Angola</b><br>Life Expectancy: 51 years<br>... |
| 2 | Africa | Benin | 59.165 | 1685.0 | 9775152.0 | <b>Benin</b><br>Life Expectancy: 59 years<br>G... |
| 3 | Africa | Botswana | 47.152 | 14905.0 | 2030287.0 | <b>Botswana</b><br>Life Expectancy: 47 years<b... |
| 5 | Africa | Burundi | 53.637 | 737.0 | 8899077.0 | <b>Burundi</b><br>Life Expectancy: 54 years<br... |
LifePopGDP.loc[0, 'text']
'<b>Algeria</b><br>Life Expectancy: 71 years<br>GDP/Capita: $12,779<br>Population: 36,488,669'
continents = LifePopGDP['Continent'].unique()
continents
array(['Africa', 'Asia', 'Europe', 'North America', 'Oceania',
'South America'], dtype=object)
traces = []
for c in continents:
traces.append({'type' : 'scatter',
'mode' : 'markers',
'name' : c,
'hoverinfo' : 'text',
'x' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'GDP per Capita'],
'y' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Life Expectancy'],
'text' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'text'],
'marker' : {'size' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Population'],
'sizeref' : 500000,
'sizemode' : 'area'}})
pyo.iplot(traces)
traces = []
for c in continents:
traces.append({'type' : 'scatter',
'mode' : 'markers',
'name' : c,
'hoverinfo' : 'text',
'x' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'GDP per Capita'],
'y' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Life Expectancy'],
'text' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'text'],
'marker' : {'size' : LifePopGDP.loc[LifePopGDP['Continent'] == c, 'Population'],
'sizeref' : 500000,
'sizemode' : 'area',
'sizemin' : 2.5}})
pyo.iplot(traces)
layout = {'title' : 'Life Expectancy and GDP per capita',
'xaxis' : {'title' : 'GDP per capita at PPP ($)',
'range' : [LifePopGDP['GDP per Capita'].min() * 0.95,
LifePopGDP['GDP per Capita'].max() * 1.05]},
'yaxis' : {'title' : 'Life expectancy (years)',
'range' : [LifePopGDP['Life Expectancy'].min() * 0.95,
LifePopGDP['Life Expectancy'].max() * 1.05]},
'hovermode' : 'closest'}
fig = Figure(data=traces, layout=layout)
pyo.iplot(fig)
difference = LifePopGDP['GDP per Capita'].max() - LifePopGDP['GDP per Capita'].min()
fig['layout']['xaxis'].update({'range' : [LifePopGDP['GDP per Capita'].min() - (difference * 0.05),
LifePopGDP['GDP per Capita'].max() * 1.05]})
pyo.iplot(fig)